package com.ccrfid.rmc6.resource.business.rollcall;

import com.ccrfid.rmc6.appconfig.auth.Secured;
import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.appconfig.exception.pojo.CustomErrors;
import com.ccrfid.rmc6.entity.business.rollcall.RollcallScheduleEntity;
import com.ccrfid.rmc6.entity.business.rollcall.RollcallTempEntity;
import com.ccrfid.rmc6.pojo.dto.rollcall.RollcallResultNode;
import com.ccrfid.rmc6.pojo.dto.rollcall.RollcallTimeDto;
import com.ccrfid.rmc6.service.business.rollcall.RollcallResultService;
import com.ccrfid.rmc6.service.business.rollcall.RollcallScheduleService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import java.util.List;

@Secured
@Path("roll-call")
@Produces(MediaType.APPLICATION_JSON)
public class RollcallResource {
    @Autowired
    private RollcallScheduleService rollcallScheduleService;
    @Autowired
    private RollcallResultService rollcallResultService;

    //获取点名计划列表
    @GET
    @Path("schedules")
    public List<RollcallScheduleEntity> getScheduleList() {
        return rollcallScheduleService.getScheduleList();
    }

    //新增点名计划时间
    @POST
    @Path("schedules")
    @Consumes(MediaType.APPLICATION_JSON)
    public RollcallScheduleEntity createSchedule(RollcallScheduleEntity entity) {
        return rollcallScheduleService.createSchedule(entity);
    }

    //更新点名计划
    @PUT
    @Path("schedules/{scheduleId}")
    public RollcallScheduleEntity updateSchedule(@PathParam("scheduleId") Long scheduleId,
                                                 RollcallScheduleEntity entity) throws AppException {
        return rollcallScheduleService.updateSchedule(scheduleId, entity);
    }

    //删除点名计划
    @DELETE
    @Path("schedules")
    @Consumes(MediaType.APPLICATION_JSON)
    public int deleteSchedules(List<Long> idList) throws AppException {
        if (idList == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.param_is_null.getCode(), CustomErrors.param_is_null.getReason(), "");
        }
        return rollcallScheduleService.deleteSchedules(idList);
    }

    @GET
    @Path("schedules/enable-status")
    public int getSchedulesStatus() {
        return rollcallScheduleService.getSchedulesStatus();

    }

    @PUT
    @Path("schedules/on")
    public int enableSchedules() {
        return rollcallScheduleService.enableSchedules();
    }

    @PUT
    @Path("schedules/off")
    public int disableSchedules() {
        return rollcallScheduleService.disableSchedules();
    }

    //获取某天有哪些点名时间；date：yyyy-MM-dd
    @GET
    @Path("{date}/times")
    public List<RollcallTimeDto> getRollcallTimeList(@PathParam("date") String dateStr) {
        return rollcallResultService.getRollcallTimeList(dateStr);
    }

    //获取某次点名的结果
    @GET
    @Path("{rollcallId}/result")
    public RollcallResultNode getRollcallResult(@PathParam("rollcallId") Long rollcallId) {
        return rollcallResultService.getRollcallResult(rollcallId);
    }

    //手动点名开始
    @POST
    @Path("manual")
    public Long manualRollcall(@Context SecurityContext securityContext) {
        String userAccount = securityContext.getUserPrincipal().getName();
        return rollcallResultService.manualRollcall(userAccount);
    }

    //获取手动点名结果
    @GET
    @Path("manual/{rollcallId}/result")
    public RollcallResultNode getManualRollcallResult(@PathParam("rollcallId") Long rollcallId) {
        return rollcallResultService.getManualRollcallResult(rollcallId);
    }

    //保存点名结果
    @PUT
    @Path("manual/{rollcallId}/result")
    public Boolean saveManualRollcallResult(@PathParam("rollcallId") Long rollcallId) {
        return rollcallResultService.saveManualRollcallResult(rollcallId);
    }

    //修改某条记录备注信息
    @PUT
    @Path("manual/result/{resultId}")
    public RollcallTempEntity updateRollcallRecord(@PathParam("resultId") Long resultId, RollcallTempEntity entity)
            throws AppException {
        return rollcallResultService.updateRollcallRecord(resultId, entity);
    }

    //----------------------------------------------------------------------
    //按部门依次点名
    //获取手动点名ID
    @GET
    @Path("manual/id")
    public Long getManualRollcallId() {
        return rollcallResultService.getManualRollcallId();
    }

    //点名某个部门
    @POST
    @Path("manual/{rollcallId}/org/{orgId}")
    public RollcallResultNode manualRollcall(@PathParam("rollcallId") Long rollcallId,
                                             @PathParam("orgId") Long orgId,
                                             @Context SecurityContext securityContext) {
        String userAccount = securityContext.getUserPrincipal().getName();
        return rollcallResultService.manualRollcall(rollcallId, orgId, userAccount);
    }


}
